package io.github.talelin.latticy.controller.api;

import io.github.talelin.latticy.common.annotation.ScopeLevel;
import io.github.talelin.latticy.dto.SendFlowerDTO;
import io.github.talelin.latticy.service.SendFlowerService;
import io.github.talelin.latticy.vo.FlowerIdVO;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.vo.UpdatedVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.Positive;

@RestController
@RequestMapping("/api/send-flower")
public class SendController
{
    @Autowired
    private SendFlowerService sendFlowerService;

    /**
     * 编辑送花
     */
    @PutMapping("/{id}")
    @ScopeLevel
    public UpdatedVO update(@RequestBody @Validated SendFlowerDTO dto,
                       @PathVariable @Positive Long id)
    {
        sendFlowerService.updated(id, dto);
        return new UpdatedVO<>();
    }

    /**
     * 送花
     */
    @PostMapping
    @ScopeLevel
    public FlowerIdVO create(@RequestBody @Validated SendFlowerDTO dto)
    {
        return sendFlowerService.create(dto);
    }

    /**
     * 我的送礼
     */
    @GetMapping("/page")
    @ScopeLevel
    public PageResponseVO page(@RequestParam(defaultValue = "0")
                               @Min(value = 0) int page,
                               @RequestParam(defaultValue = "20")
                               @Min(value = 1) int size)
    {
        return sendFlowerService.getFlowers(page, size);
    }

}
